package de.op.projectc.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import de.op.projectc.MainGame;
import de.op.projectc.stats.Options;

public class MainMenuScreen implements Screen {

	final MainGame game;
	private Stage stage;
	private Skin skin;

	public MainMenuScreen(final MainGame game) {
		this.game = game;
		
		stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
	    skin = new Skin(Gdx.files.internal("ui/uiskin.json"));

        Table table = new Table(skin);
        table.setFillParent(true);
        table.setBounds(0, 0, stage.getWidth(), stage.getHeight());
        table.center();
        table.defaults().pad(5);

        Label labelTitel = new Label("Hauptmenue", skin);
        labelTitel.setFontScale(2);
        table.add(labelTitel).width(labelTitel.getPrefWidth()).spaceBottom(15).row();

        //TODO groessten Button berechnen und andere anpassen
        TextButton button = new TextButton("Spiel starten", skin);
        button.addListener(new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                stage.addAction(Actions.sequence(Actions.moveBy(-stage.getWidth(), 0, .5f), Actions.run(new Runnable() {
                            public void run() {
                                game.setScreen(new GameScreen(game));
                            }
                        })
                ));
            }
        });
        table.add(button).width(table.getWidth() / 4).row();

        button = new TextButton("Optionen", skin);
        button.addListener(new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                stage.addAction(Actions.sequence(Actions.moveBy(-stage.getWidth(), 0, .5f), Actions.run(new Runnable() {
                            public void run() {
                                game.setScreen(new OptionScreen(game));
                            }
                        })
                ));
            }
        });
        table.add(button).width(table.getWidth() / 4).row();

        button = new TextButton("Spiel beenden", skin);
        button.addListener(new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                stage.addAction(Actions.sequence(Actions.fadeOut(1), Actions.run(new Runnable() {
                            public void run() {
                                Gdx.app.exit();
                            }
                        })
                ));
            }
        });
        table.add(button).width(table.getWidth() / 4).row();

        table.debug();
	    stage.addActor(table);

        stage.addAction(Actions.sequence(Actions.moveBy(-stage.getWidth(), 0), Actions.moveBy(stage.getWidth(), 0, .5f)));
	}
	
	@Override
	public void dispose() {
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void render(float delta) {
        Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act(delta);
        stage.draw();

        if(Options.getInstance().isDEBUG()) {
            Table.drawDebug(stage);
        }
    }

	@Override
	public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
	}

	@Override
	public void resume() {
	}

	@Override
	public void show() {
        Gdx.input.setInputProcessor(stage);
	}
}
